package com.jxw.launcher.util;

import android.content.Context;
import android.net.http.Headers;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import cn.qqtheme.framework.util.ConvertUtils;
import com.zzj.filedownloader.download.DownLoadManager;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import net.lingala.zip4j.util.InternalZipConstants;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/* loaded from: classes.dex */
public class CheckFileAndDown {
    public static String SP_FILE_ARRAY = "sp_file_array";
    private static final String TAG = "CheckFileAndDown";
    static String dir = null;
    private static final String downUrl = "http://ksyunpub.zhinengtongbu.com/apkfilelist";
    public static boolean isAllDownSus = true;
    static String oem_dir;
    static List<String> paths = new ArrayList();

    public static List<String> checkFile(Context context, JSONArray jSONArray) {
        try {
            int length = jSONArray.length();
            File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/ansystem");
            if (!file.exists()) {
                file.mkdirs();
            }
            dir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ansystem/";
            paths.clear();
            String spString = com.jxw.utils.SharedPreferencesUtil.getSpString(context, SP_FILE_ARRAY, "");
            HashMap hashMap = new HashMap();
            if (TextUtils.isEmpty(spString)) {
                Log.i("downloadfile ", "TextUtils.isEmpty(localFile): ");
                try {
                    JSONArray jSONArray2 = new JSONArray(spString);
                    for (int i = 0; i < jSONArray2.length(); i++) {
                        JSONObject optJSONObject = jSONArray.optJSONObject(i);
                        String optString = optJSONObject.optString("filename");
                        hashMap.put(optString, optJSONObject.optString(Headers.ETAG));
                        String[] split = optString.split(InternalZipConstants.ZIP_FILE_SEPARATOR);
                        String str = dir + split[0];
                        Log.i("downloadfile", "checkFile:address " + str);
                        File file2 = new File(str);
                        if (!file2.exists()) {
                            file2.mkdirs();
                        }
                    }
                } catch (JSONException e) {
                    Log.i(TAG, "downloadfile: 异常");
                    e.printStackTrace();
                }
            }
            for (int i2 = 0; i2 < length; i2++) {
                JSONObject optJSONObject2 = jSONArray.optJSONObject(i2);
                String optString2 = optJSONObject2.optString("filename");
                String optString3 = optJSONObject2.optString(Headers.ETAG);
                String optString4 = optJSONObject2.optString("size");
                if (!TextUtils.isEmpty(optString4)) {
                    Long.parseLong(optString4);
                    if (TextUtils.isEmpty(spString)) {
                        Log.i("downloadfile ", "TextUtils.isEmpty(localFile)");
                        if (!queryFileIsExist(dir + optString2)) {
                            if ((dir + optString2).equals(Environment.getExternalStorageDirectory().getAbsolutePath() + "/ansystem/词典/学王词典.JWDIC")) {
                                Log.i(TAG, "downloadfile 2-1:  进入下载 " + dir + optString2);
                                isAllDownSus = false;
                                paths.add(optString2);
                            }
                        }
                        Log.i(TAG, "downloadfile 2-2:  不需要下载 ");
                    } else if (hashMap.containsKey(optString2)) {
                        Log.i("downloadfile ", "localMap.containsKey(path)");
                        if (!((String) hashMap.get(optString2)).equals(optString3)) {
                            Log.i(TAG, "downloadfile: !localEtag.equals(etag：" + optString2);
                            isAllDownSus = false;
                            paths.add(optString2);
                        }
                    } else {
                        if (!queryFileIsExist(dir + optString2)) {
                            if ((dir + optString2).equals(Environment.getExternalStorageDirectory().getAbsolutePath() + "/ansystem/词典/学王词典.JWDIC")) {
                                Log.e("downloadfile 3-1 ", " 进入下载 " + dir + optString2);
                                isAllDownSus = false;
                                paths.add(optString2);
                            }
                        }
                        Log.i(TAG, "downloadfile 3-2:  不需要下载 ");
                    }
                }
            }
        } catch (Exception e2) {
            e2.printStackTrace();
        }
        return paths;
    }

    public static List<String> checkFile1(Context context, JSONArray jSONArray) {
        int i;
        String str;
        String str2;
        String str3;
        JSONArray jSONArray2 = jSONArray;
        try {
            int length = jSONArray.length();
            Log.i("downloadfile", "网络返回长度: " + length);
            File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/ansystem");
            if (!file.exists()) {
                file.mkdirs();
            }
            dir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ansystem/";
            oem_dir = "/mnt/oem/ansystem/";
            paths.clear();
            String spString = com.jxw.utils.SharedPreferencesUtil.getSpString(context, SP_FILE_ARRAY, "");
            HashMap hashMap = new HashMap();
            boolean isEmpty = TextUtils.isEmpty(spString);
            String str4 = Headers.ETAG;
            String str5 = "filename";
            if (isEmpty) {
                Log.i("downloadfile ", "TextUtils.isEmpty(localFile): ");
                try {
                    JSONArray jSONArray3 = new JSONArray(spString);
                    for (int i2 = 0; i2 < jSONArray3.length(); i2++) {
                        JSONObject optJSONObject = jSONArray2.optJSONObject(i2);
                        String optString = optJSONObject.optString("filename");
                        hashMap.put(optString, optJSONObject.optString(Headers.ETAG));
                        String[] split = optString.split(InternalZipConstants.ZIP_FILE_SEPARATOR);
                        String str6 = dir + split[0];
                        String str7 = oem_dir + split[0];
                        Log.i("downloadfile", "checkFile:address " + str6);
                        File file2 = new File(str6);
                        if (!file2.exists()) {
                            file2.mkdirs();
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            int i3 = 0;
            while (i3 < length) {
                JSONObject optJSONObject2 = jSONArray2.optJSONObject(i3);
                String optString2 = optJSONObject2.optString(str5);
                Log.i("downloadfile", "path名: " + optString2);
                String optString3 = optJSONObject2.optString(str4);
                String optString4 = optJSONObject2.optString("size");
                if (TextUtils.isEmpty(optString4)) {
                    i = length;
                    str = spString;
                    str2 = str4;
                    str3 = str5;
                } else {
                    Log.i("downloadfile ", "!strSize：" + optString4);
                    long parseLong = Long.parseLong(optString4);
                    if (TextUtils.isEmpty(spString)) {
                        Log.i("downloadfile ", "TextUtils.isEmpty(localFile)");
                        i = length;
                        str = spString;
                        if (queryFileIsExist(dir + optString2)) {
                            str2 = str4;
                            str3 = str5;
                            long fileOrFilesSize = (long) FileSizeUtil.getFileOrFilesSize(dir + optString2, 1);
                            Log.i("downloadfile", "checkFile:dir +path: " + dir + "::::::" + optString2);
                            Log.e("downloadfile", optString2 + ",fileSize:" + fileOrFilesSize + ",webSize:" + parseLong);
                            if (fileOrFilesSize != parseLong) {
                                Log.e("downloadfile", "大小不一样");
                                isAllDownSus = false;
                                paths.add(optString2);
                            }
                        } else {
                            str2 = str4;
                            str3 = str5;
                            if (queryFileIsExist(oem_dir + optString2)) {
                                long fileOrFilesSize2 = (long) FileSizeUtil.getFileOrFilesSize(oem_dir + optString2, 1);
                                Log.i("downloadfile", "checkFile:dir +path: " + oem_dir + "::::::" + optString2);
                                Log.e("downloadfile", optString2 + ",fileSize:" + fileOrFilesSize2 + ",webSize:" + parseLong);
                                if (fileOrFilesSize2 != parseLong) {
                                    Log.e("downloadfile", "大小不一样");
                                    isAllDownSus = false;
                                    paths.add(optString2);
                                }
                            } else {
                                Log.i("downloadfile ", "加入下载");
                                isAllDownSus = false;
                                paths.add(optString2);
                            }
                        }
                    } else {
                        i = length;
                        str = spString;
                        str2 = str4;
                        str3 = str5;
                        if (hashMap.containsKey(optString2)) {
                            Log.i("downloadfile ", "localMap.containsKey(path)");
                            if (!((String) hashMap.get(optString2)).equals(optString3)) {
                                isAllDownSus = false;
                                paths.add(optString2);
                            }
                        } else {
                            if (queryFileIsExist(dir + optString2)) {
                                Log.i("downloadfile", "queryFileIsExist:dir +path: " + dir + "::::::" + optString2);
                                if (((long) FileSizeUtil.getFileOrFilesSize(dir + optString2, 1)) != parseLong) {
                                    isAllDownSus = false;
                                    paths.add(optString2);
                                }
                            } else {
                                if (queryFileIsExist(oem_dir + optString2)) {
                                    Log.i("downloadfile", "queryFileIsExist:dir +path: " + oem_dir + "::::::" + optString2);
                                    if (((long) FileSizeUtil.getFileOrFilesSize(oem_dir + optString2, 1)) != parseLong) {
                                        isAllDownSus = false;
                                        paths.add(optString2);
                                    }
                                } else {
                                    Log.i("downloadfile ", "加入下载");
                                    isAllDownSus = false;
                                    paths.add(optString2);
                                    i3++;
                                    jSONArray2 = jSONArray;
                                    length = i;
                                    spString = str;
                                    str4 = str2;
                                    str5 = str3;
                                }
                            }
                        }
                    }
                }
                i3++;
                jSONArray2 = jSONArray;
                length = i;
                spString = str;
                str4 = str2;
                str5 = str3;
            }
        } catch (Exception e2) {
            e2.printStackTrace();
        }
        return paths;
    }

    private static boolean queryFileIsExist(String str) {
        return new File(str).exists();
    }

    public static void downFile(List<String> list, int i, DownLoadManager downLoadManager) {
        String str = "/ansystem/" + list.get(i);
        String str2 = downUrl + str;
        Log.i(TAG, "downFilsed: ee" + str2);
        Log.e("ttttt", str2);
        File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + str);
        try {
            if (file.exists()) {
                Logger.getLogger().d("deleteTask=" + file.getAbsolutePath());
                file.delete();
            }
            File file2 = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + str + ".tmp");
            if (file2.exists()) {
                Logger.getLogger().d("deleteTask=" + file2.getAbsolutePath());
                file2.delete();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        downLoadManager.setSupportBreakpoint(true);
        downLoadManager.addTask(str, str2, file.getName(), file.getAbsolutePath());
        downLoadManager.startTask(str);
    }

    public static String readCheckFile(Context context) {
        String str = "";
        try {
            str = new JSONArray(ConvertUtils.toString(context.getAssets().open("check.json"))).toString().replace("[", "").replace("]", "").replace("\\", "").replace("\"", "");
            Log.i("===checkMd5=====", str);
            return str;
        } catch (Exception e) {
            e.printStackTrace();
            return str;
        }
    }
}
